

var app = getApp();
var result = false;
var shareId = -1;
/**
 * 用户点击右上角分享
 * datas对象包含 栏目标题title，栏目地址 path、栏目图片 imageUrl ；；programId栏目id 
 */
function onShare(that, datas) {

	//分享时首先需要执行调用服务接口执行插入操作，成功后返回id带到分享链接后边即可TODO。。。。
	console.log(datas)
	 wx.showShareMenu({
	   withShareTicket: true
	 })
	console.log(wx.getStorageSync("userInfo").nickName)

	return {
		title: wx.getStorageSync("userInfo").nickName + '邀您一起听',
		path: datas.path + "?userId="+wx.getStorageSync("userId"),
		imageUrl: datas.imageUrl,
		success(res) {
			//分享成功 
			console.log(res)
			//分享成功
			shareTickets = res.shareTickets;
			console.log("shareTickets=" + shareTickets)
			console.log("进入result" + result)
			shareId = res.id;
			console.log("拿到shareId"+shareId)
			
		},
		fail(e) {
			console.log("fail")
		},
		//分享完成
		complete() {
			console.log("complete")
		}
	} 
	
	if (shareTickets == undefined) { //执行个人分享保存
		console.log("进入个人。。。。。。。。。。。。。。")
		//分装提交数据 shareSource 栏目id  shareStatus分享状态、shareSource分享原0用户1群
		var data = {
			userId: wx.getStorageSync("userId") ? undefined : -1,
			programId: datas.programId,
			shareSource: 0, //个人分享
			//shareStatus: "1"
		}
		singleGroupId(data); //调取保存用户分享的栏目
		console.log("分享成功success")

	} else { //执行群分享保存
		var datas = {
			userId: wx.getStorageSync("userId")?undefined:-1,
			programId: datas.programId,
			shareSource: "1", //群分享
			//shareStatus: "1"
		}
		getGroupID(datas); //调用获取群编号的方法
	}
 
}

/**
 * 个人用户分享
 */
function singleGroupId(data) {
	app.request(app.globalData.https + "programResource/myProgramShare", //处理获取分享群信息
		"post", data,
		function (res) {
			console.log(res)
			if (res.code == 0) {
				console.log("个人分享result")
				console.log(res)
				shareId = res.id;
				result = true;
			}
		},
		function (res) {
			console.log(res)
		}
	)
}


/**
 * 群组用户分享
 */
function getGroupID(datas) { 
	wx.getShareInfo({
		shareTicket: shareTickets[0],
		success: function (res) {
			console.log("=================*===============")
			console.log(shareTickets[0])
			console.log("==================*==============")
			var encryptedData = res.encryptedData;
			console.log(res)
			var iv = res.iv;
			wx.login({
				success: function (res) {
					var code = res.code;
					console.log(code)
					var userId = wx.getStorageSync("userId") ? undefined : -1;
					var programId = datas.programId;
					var shareSource = datas.shareSource;
					//var shareStatus = datas.shareStatus;
					//var survivalTime = datas.survivalTime;
					var data = {
						'wxEncryptedData': encryptedData,
						"wxIv": iv,
						"userId": userId,
						"programId": programId,
						"shareSource": shareSource,
						"wxCode": code
					};
					console.log(data)
					//处理获取分享群信息
					app.request(app.globalData.https + "programResource/myProgramShare",
						"post", data,
						function (res) {
							console.log("群组分享")
							console.log(res)
							shareId = res.id;
							result = true;
						},
						function (res) {
							console.log(res)
						}
					)
				}
			})
		}
	})
}



module.exports = {
	onShare: onShare
}